<template>
	<view class="container">
		<u-navbar title='回访问卷' :title-bold='true' :border-bottom='false'></u-navbar>
		<u-search bg-color='#FFF' @change="search" margin="0" class="search" placeholder="搜索姓名" :show-action="false"
			v-model="form.keyword"></u-search>
		<view class="topic">
			<uni-segmented-control :current="0" :values="topic" @clickItem="select" styleType="text"
				activeColor="#6EC9E1"></uni-segmented-control>
		</view>

		<view class="content" v-for="(item,idx) in list" :key="idx">
			<view class="card">

				<view class="center">
					<view class="userInfo" @click="goTo('/sub/center/mine/huifangDetail?id='+item.id)">
						<view class="avatar">{{item.member_info.username?item.member_info.username.substring(0,1):''}}
						</view>
						<view class="dsc">
							<view class="d1">
								<view class="">
									<span class="s1">{{item.member_info.username||''}}</span>

									<block v-if="user.job==1">
										<u-tag size='mini' v-if="item.read==0" mode="plain" :closeable='false' text="未读"
											type="info" />
									</block>
									<block v-else>
										<u-tag size='mini' v-if="item.read_doctor==0" mode="plain" :closeable='false'
											text="未读" type="info" />
									</block>
								</view>

								<span class="statu">{{item.day_text+item.status_text}}</span>
							</view>
							<view class="d2">
								<span>{{item.createtime}}</span>
							</view>
						</view>
					</view>
					<!-- 	<view class="userInfo" v-else>
						<u-avatar  :src="item.user_info.avatar"></u-avatar>
						<view class="dsc">
							<view class="d1">
								<view class="d1">
									<span class="s1">{{item.user_info.username}}</span>
									<u-tag style="opacity: 0.6;" :text="item.read_text" :closeable='false' mode="plain" size='mini' 
										:type="item.read_text=='未读'?'error':'info'"  />
								</view>
								
								<span class="statu">{{item.status_text}}</span>
							</view>
							<view class="d2">
								<span>{{item.createtime}}</span>
							</view>
						</view>
					</view> -->
					<view class="button">
						<!-- 用户端 -->
						<view class="b-s"  @click.stop="toagain(item.id)">再访一次</view>
						<view class="b-s" v-if="user.job==1" @click.stop="tochat(item.doctor_id)">护航员</view>
						<view class="b-s" v-else @click.stop="tochat(item.user_id)">联系用户</view>
						<view class="b-s" @tap="goTo('/sub/center/mine/huifangDetail?id='+item.id)">查看详情</view>
					</view>
				</view>
			</view>
		</view>
		<view class="notDta" v-if="list.length<1">
			暂无数据
		</view>
		<!-- 提示 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		followuserList,
		followdoctorList,
		addcopyfollow,
		chat
	} from '@/api/index.js'
	export default {
		data() {
			return {
				list: [],
				topic: [],
				t_index: 0,
				user: {},
				form: {
					page: 1,
					per_page: 10,
					see_user_id: '',
					keyword: '',
					disease: '',
					read: ''
				},

			}
		},
		onLoad(e) {
			if (e.id) {
				this.form.see_user_id = e.id
			}
		},
		onShow() {
			this.form.page = 1
			this.list = []
			this.init()
		},
		onReachBottom() { //监听页面触底函数
			this.init()
		},
		methods: {
			//再次回访
			toagain(id){
				let that = this
				uni.showModal({
					title: '再访一次',
					content: '是否再访一次',
					success: function(res) {
						if (res.confirm) {
							addcopyfollow({
								'id': id
							}).then(res => {
								if (res.code == 1) {
									that.form.page = 1
									that.list = []
									that.init()
								}else{
									uni.showToast({
										title: res.msg,
										icon: 'error'
									})
								}
							})
				
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			init() {
				let that = this;
				that.user = that.ws.getUserInfo()
				let arr = []
				if (that.user.job == 2) {
					followdoctorList(that.form).then(res => {
						if (res.code == 1) {
							that.topic = []
							arr = Object.values(res.data.disease)
							that.topic.push('全部')
							that.topic.push('未读')
							// that.topic.push('待阅读')
							// -------------
							arr.map(item => {
								that.topic.push(item)
							})
							// -----------------
							that.list = [...that.list, ...res.data.list.data]
							if (res.data.list.data.length > 0) {
								that.form.page++
							}
						} else {
							that.$refs.uToast.show({
								title: res.msg
							})
						}

					})
				} else {
					followuserList(that.form).then(res => {
						if (res.code == 1) {
							that.topic = []
							that.topic.push('全部')
							if (that.user.job == 2) {
								that.topic.push('未读')
							}
							that.topic.push('未提交')
							arr = Object.values(res.data.disease)
							// -------------
							that.topic.push('已提交')

							// arr.map(item=>{
							// 	that.topic.push(item)
							// })
							// -----------------

							that.list = [...that.list, ...res.data.list.data]
							if (res.data.list.data.length > 0) {
								that.form.page++
							}
						} else {
							that.$refs.uToast.show({
								title: res.msg
							})
						}

					})
				}


			},

			select(e) {
				this.t_index = e.currentIndex
				this.form.page = 1
				this.form.keyword = ''
				this.form.read = ''
				this.form.disease = ''
				this.list = []
				if (this.user.job == 2) {
					switch (this.t_index) {
						case 1:
							this.form.read = 0;
							break;
							// case 2:
							// 	this.form.read = 1;
							// 	break;
						case 2:
							this.form.disease = 1;
							break;
						case 3:
							this.form.disease = 2;
							break;
						case 4:
							this.form.disease = 3;
							break;
						case 5:
							this.form.disease = 4;
							break;
					}
				} else {
					switch (this.t_index) {
						case 1:
							this.form.status = 0;
							break;
						case 2:
							this.form.status = 1;
							break;
						case 3:
							this.form.disease = 2;
							break;
					}
				}

				this.init()
			},
			search() {
				this.form.page = 1
				this.form.read = ''
				this.form.disease = ''
				this.list = []
				this.init()
			},

			goTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			tochat(id) {
				let that = this
				chat({
					'user_id': id
				}).then(res => {
					if (res.code == 1) {
						that.ws.tochat(res.data.session_id)
					}
				})
			}

		}
	}
</script>

<style lang="less" scoped>
	.container {
		height: 100vh;
		padding: 15px;

		.u-nav-slot {
			display: flex;
		}

		.avatar {
			min-width: 84rpx;
			min-height: 84rpx;
			width: 84rpx;
			height: 84rpx;
			border-radius: 50%;
			color: #FFF;
			font-size: 18px;
			background-color: #6EC9E1;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.notDta {
			height: 60px;
			line-height: 60px;
			text-align: center;
			margin-top: 40rpx;
		}

		.topic {
			margin: 20rpx 0;
		}

		.content {
			margin-top: 20rpx;

			.card {
				padding: 20rpx 30rpx;
				background-color: #FFF;
				border-radius: 20rpx;


				.center {
					font-size: 14px;

					.userInfo {
						margin: 30rpx 0;
						display: flex;
						align-items: center;


						.dsc {
							width: 100%;
							margin-left: 16rpx;
							line-height: 20px;

							span {
								opacity: 0.6;
								font-size: 12px;
							}

							.d1 {
								display: flex;
								justify-content: space-between;
								align-items: center;

								view {
									display: flex;
									align-items: center;

									.s1 {
										margin-right: 20rpx;
										font-size: 16px;
										font-weight: 550;
										opacity: 1;
									}
								}

								.statu {
									opacity: 1;
									font-size: 13px;
									font-weight: bold;
								}
							}

							.d2 {
								display: flex;
								align-items: center;
								margin-top: 10rpx;

								span {
									margin-right: 10rpx;
								}

							}

						}
					}
				}
			}
		}

	}
</style>